<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form-model
        ref="formModel"
        :model="form"
        layout="inline"
        :rules="{
          name: [
            {
              validator: validators.noWhitespace(),
              trigger: 'change',
            },
            {
              validator: validators.noIllegalString(),
              trigger: 'change',
            },
            {
              max: 10,
              message: '长度在 1 到 10 个字符',
              trigger: 'change',
            },
          ],
        }"
      >
        <a-row :gutter="[16, 16]" type="flex" justify="end">
          <a-col :md="6" :sm="8">
            <a-form-model-item prop="name" label="服务器名称">
              <a-input autocomplete="off" v-model="form.name" />
            </a-form-model-item>
          </a-col>
          <span
            style="float: left; overflow: hidden"
            class="table-page-search-submitButtons"
          >
            <a-col :md="12" :sm="24">
              <a-button
                type="primary"
                @click="loadData(true)"
                icon="search"
                style="margin-left: 21px"
                >查询</a-button
              >
              <a-button
                icon="reload"
                style="margin-left: 8px"
                @click="resetSearchForm"
                >重置</a-button
              >
            </a-col>
          </span>
        </a-row>
      </a-form-model>
    </div>
    <a-table
      ref="table"
      rowKey="id"
      size="middle"
      :columns="columns"
      :dataSource="gDataSource"
      :pagination="gPagination"
      @change="gHandleTableChange"
    >
      <span slot="sortCol" slot-scope="text, record, index">
        {{
          (gPagination.current - 1) * gPagination.pageSize + parseInt(index) + 1
        }}
      </span>
      <span slot="opinionCol" slot-scope="text, record, index">
        <span
          v-if="
            (gPagination.current - 1) * gPagination.pageSize +
              parseInt(index) +
              1 <
            6
          "
          style="color: green"
        >
          推荐
        </span>
        <span v-else style="color: red"> 不推荐 </span>
      </span>
    </a-table>
  </a-card>
</template>

<script>
import { GwListMixin } from "@/mixins/GwListMixin";
import { gPostAction } from "@/api/manage";

export default {
  name: "F010206",
  mixins: [GwListMixin],
  components: {},
  data() {
    return {
      // 表头
      columns: [
        {
          title: "序号",
          dataIndex: "",
          key: "rowIndex",
          width: 50,
          scopedSlots: { customRender: "sortCol" },
        },
        {
          title: "服务器名称",
          dataIndex: "name",
        },
        {
          title: "电源状态",
          dataIndex: "powerStatusCnName",
        },
        {
          title: "带外管理IP",
          dataIndex: "ipmiIp",
        },
        {
          title: "磁盘读速率",
          dataIndex: "readRate",
          customRender: function (t, r, index) {
            return t ? t : "加载中...";
          },
        },
        {
          title: "磁盘写速率",
          dataIndex: "writeRate",
          customRender: function (t, r, index) {
            return t ? t : "加载中...";
          },
        },
        {
          title: "服务器功耗",
          dataIndex: "powerConsumption",
          customRender: function (t, r, index) {
            return t + " W";
          },
        },
        {
          title: "存储剩余率",
          dataIndex: "storageCapacity",
          customRender: function (t, r, index) {
            return (t * 100).toFixed(2) + "%";
          },
        },
        {
          title: "推荐意见",
          key: "rowOpinion",
          scopedSlots: { customRender: "opinionCol" },
        },
      ],
      form: {
        name: "",
      },
      url: {
        list: "/asset/pageNode",
      },
      diskWriteRateMap: {},
      diskReadRateMap: {},
      diskWriteRateList: [],
      diskReadRateList: [],
    };
  },
  mounted() {
    this.loadData();
  },
  watch: {
    diskWriteRateList(val) {
      if (val && val.length === this.gDataSource.length) {
        let list = [];
        this.gDataSource.forEach((item) => {
          item.writeRate = this.diskWriteRateMap[item.id];
          item.readRate = this.diskReadRateMap[item.id];
          list.push(item);
        });
        this.gDataSource = list;
      }
    },
    diskReadRateList(val) {
      if (val && val.length === this.gDataSource.length) {
        let list = [];
        this.gDataSource.forEach((item) => {
          item.writeRate = this.diskWriteRateMap[item.id];
          item.readRate = this.diskReadRateMap[item.id];
          list.push(item);
        });
        this.gDataSource = list;
      }
    },
  },
  methods: {
    loadData(isDoSearch) {
      this.$refs.formModel.validate((error) => {
        if (!error) {
          return;
        }
        this.gQueryParam.name = this.form.name;
        this.gQueryParam.order = "storage_capacity";
        this.gQueryParam.title = "精确管控与供电制冷联动-精确能耗管控-动态存储";
        this.gQueryParam.content = "查询动态存储服务器列表"
        if (isDoSearch) {
          this.gQueryParam.pageNo = 1;
          this.gPagination.current = 1;
        }
        const _this = this;
        _this.gDataSource = [];
        _this.diskWriteRateList = [];
        _this.diskReadRateList = [];
        _this.gPagination.total = 0;
        gPostAction(_this.url.list, _this.gQueryParam).then((res) => {
          if (res.data && res.data.list && res.data.list.length) {
            _this.gDataSource = res.data.list;
            _this.gPagination.total = res.data.totalCount;
            res.data.list.forEach((item) => {
              gPostAction("/statistics/latestSamples", {
                meter: "host.disk_read_rate",
                resourceId: item.id,
              }).then((read) => {
                if (read.data && read.data.length) {
                  _this.diskReadRateMap[item.id] =
                    Math.floor(read.data[0].volume) + "KB/s";
                  _this.diskReadRateList.push({
                    id: item.id,
                    readRate: Math.floor(read.data[0].volume) + "%",
                  });
                } else {
                  _this.diskReadRateMap[item.id] = "";
                  _this.diskReadRateList.push({
                    id: item.id,
                    readRate: "",
                  });
                }
              });
              gPostAction("/statistics/latestSamples", {
                meter: "host.disk_write_rate",
                resourceId: item.id,
              }).then((write) => {
                if (write.data && write.data.length) {
                  _this.diskWriteRateMap[item.id] =
                    Math.floor(write.data[0].volume) + "KB/s";
                  _this.diskWriteRateList.push({
                    id: item.id,
                    writeRate: Math.floor(write.data[0].volume) + "%",
                  });
                } else {
                  _this.diskWriteRateMap[item.id] = "";
                  _this.diskWriteRateList.push({
                    id: item.id,
                    writeRate: "",
                  });
                }
              });
            });
          }
        });
      });
    },
    resetSearchForm() {
      this.form.name = "";
      this.$refs.formModel.resetFields();
    },
  },
};
</script>
<style scoped>
</style>
